<template>
    <div class="app-container"
         v-loading.fullscreen.lock="allLoading"
         element-loading-text="拼命处理中,请耐心等待"
         element-loading-spinner="el-icon-loading"
         element-loading-background="rgba(0, 0, 0, 0.8)"
    >
        <el-dialog :close-on-click-modal="changeBitFlagIsNone" :title="mainDialogInfo.title" :visible.sync="showDialog"
                   width="100%" append-to-body @close="closeDialog">
            <el-form ref="form" :disabled="changeBitFlagIsNone" :model="form" :rules="rules" label-width="120px">
            </el-form>
            <el-row>
                <el-input type="textarea" :rows="10"   placeholder="请输入内容" v-model="form.jsonCode"></el-input>
            </el-row>
            <el-row :gutter="10" class="mb10 mt10">
                <el-col :span="1.5">
                    <el-button type="primary" @click="handleJsonToJavaBean()">json转JavaBean</el-button>
                </el-col>
<!--                <el-col :span="1.5">-->
<!--                    <el-button type="primary" @click="handleSqlToInitTableInfo(2)">mybatis_insert_Attr</el-button>-->
<!--                </el-col>-->
<!--                <el-col :span="1.5">-->
<!--                    <el-button type="primary" @click="handleSqlToInitTableInfo(3)">mybatis_insert_values</el-button>-->
<!--                </el-col>-->
<!--                <el-col :span="1.5">-->
<!--                    <el-button type="primary" @click="handleSqlToInitTableInfo(4)">mybatis_update</el-button>-->
<!--                </el-col>-->
            </el-row>
            <el-row>
                <!--        class="sql11"-->
                <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="form.code" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
<!--                <el-input :rows="10"  readonly type="textarea"  v-model="form.code"></el-input>-->
                <pre><code class="hljs" v-html="form.codeHtml"></code></pre>
            </el-row>
            <div slot="footer" class="dialog-footer">
                <template v-if="!changeBitFlagIsNone">
                    <el-button type="primary" @click="submitMainDialog">确 定</el-button>
                </template>
                <el-button @click="cancelMainDialog">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import request from "/src/utils/request";

import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css";
import {sksDialogFormMixin} from "sks-plugin-el-erp/lib/dialog-form";
hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("vue", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("javascript", require("highlight.js/lib/languages/javascript"));
hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));


// 预览生成的分组代码
export function jsonToBean(data) {
    return request({
        url: '/tool/gen/jsonToBean',
        method: 'post',
        data: data,
    })
}


export default {
    name: "JsonToBeanDialog",
    mixins: [
        sksDialogFormMixin,
    ],
    data() {
        return {
            defaultForm: {
                jsonCode: undefined,
                code: undefined,
                codeHtml: undefined,
            },
            defaultFormNotNull: {
            },

            rules: {
            },
        };
    },
    methods:{
        initMainDialogInfo(){
            return {
                title: 'json转Bean',
            }
        },
        validateSuccess() {
            let data = this.mergeFromObject({}, this.form);

            if (!this.isNullOrUndefined(this.mainDialogInfo.submitCallback)) {
                this.mainDialogInfo.submitCallback({submitData: data, ref: this})
                //this.showDialog=false
                return
            }
        },
        /** 高亮显示 */
        highlightedCode(code, language) {
            const result = hljs.highlight(language, code || "", true);
            return result.value || '&nbsp;';
        },
        /** 复制代码成功 */
        clipboardSuccess(){
            this.$modal.msgSuccess("复制成功");
        },
        handleJsonToJavaBean(){
            let data=this.form.jsonCode
            this.form.codeHtml=undefined
            this.form.code=undefined
            jsonToBean(data).then(res=>{
                this.form.code=res.data.java
                this.form.codeHtml=this.highlightedCode(this.form.code,'java')
            })
        },
    }
}
</script>

<style scoped>

</style>
